using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;

namespace OdsControllerLib {

   public class OdsMainObj {
      #region fields
      //[NonSerialized]
      //OdsMainState _mainState;
      [NonSerialized]
      OdsSettingsObj _settings;
      [NonSerialized]
      OdsHostsCollectionObj _hosts;
      [NonSerialized]
      OdsMainLogObj _log;
      #endregion fields

      #region properties
      [XmlElement("OdsServerSettings")]
      public OdsSettingsObj Settings {
         get { return _settings; }
         set { _settings = value; }
      }

      public OdsHostsCollectionObj Hosts {
         get { return _hosts; }
      }
      //public OdsMainState MainState {
      //  get { return _mainState; }
      //  set { _mainState = value; }
      //}
      public OdsMainLogObj Log {
         get { return _log; }
         set { _log = value; }
      }
      #endregion properties

      /// <summary>
      /// Creates an instance of OdsMainObj - the settings, hosts and logfile are not loaded
      /// </summary>
      /// <param name="settingsFile"></param>
      /// <param name="hostsFile"></param>
      /// <param name="logFile"></param>
      public OdsMainObj(string settingsFile, string hostsFile, string logFile, string eventlogname, string eventLogSource) {
         _settings = new OdsSettingsObj();
         _settings.SettingsFile = settingsFile;

         _hosts = new OdsHostsCollectionObj();
         _hosts.HostsFile = hostsFile;
         _log = OdsMainLogObj.Instance;
         _log.LogObjSetup(logFile, eventlogname, eventLogSource);
      }

      /// <summary>
      /// Loads all the xml files: settings, hosts and the log file
      /// </summary>
      public void LoadAll() {
         _settings.LoadFromXML();
         _hosts.LoadFromXML();
         _log.ReadLogFile();
      }

      #region public methods
      public void SetHosts(OdsHostsCollectionObj newHosts) {
         //for (int i = 0; i < newHosts.Count; i++) {
         //  if (!newHosts[i].ValidateRecord()) { 

         //  }
         //}
         // TODO: Validar
         this._hosts = newHosts;
      }

      /// <summary>
      /// Adds or Replaces the hosts on this object with the ones on the ODS service account
      /// </summary>
      /// <param name="replace">replace existing hosts</param>
      public void SyncHostsFromServer(bool replace) {
         OdsClientObj myclient = new OdsClientObj(Settings, Log);
         OdsHostsCollectionObj ohosts = (OdsHostsCollectionObj)Hosts.Clone();

         Log.Write("- - - - - Adding hosts from ODS records - - - - -", LogCategories.ApplicationEvents, DateTime.Now);

         if (replace)
            ohosts.Clear();

         try {
            myclient.Connect();
            myclient.Login();
            myclient.ListRR(ref ohosts);
            myclient.Disconnect();

            SetHosts(ohosts);
         } catch (OdsClientException odse) {
            if (null != odse.InnerException)
               throw new Exception(odse.Message + "\n\n" + odse.InnerException.Message);
            else
               throw new Exception(odse.Message);
         }
      }

      /// <summary>
      /// Query the registry value that holds the instalationdir for the executables
      /// Set at install time
      /// </summary>
      /// <returns></returns>
      public static string GetInstallPathFromRegistry() {
         Microsoft.Win32.RegistryKey system, OdsController;

         string path = "";

         try {
            //Open the HKEY_LOCAL_MACHINE\SOFTWARE\OdsController key
            system = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE");
            OdsController = system.OpenSubKey("OdsController", false);
            path = OdsController.GetValue("InstallDir").ToString();

            OdsController.Close();
            system.Close();


         } catch { }

         return path;
      }

      #endregion public methods
   }
}
